package system

import (
	consts2 "MoSkeleton/config/consts"
	"MoSkeleton/framework/core/consts"
	"MoSkeleton/framework/core/dto/response"
	"MoSkeleton/framework/utils"
	"MoSkeleton/global"
	"MoSkeleton/services"
	"MoSkeleton/web/pogos/request/system"
	"MoSkeleton/web/v1/http/base"
	"fmt"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"strconv"
	"strings"
	"time"
)

type SysPostCtrl struct {
	base.BaseCtrl
}

func (s *SysPostCtrl) GetCtrlTitle() string {
	msg, _ := utils.Translater.Translate(consts.TKey_TitleSysPostMsg)
	return msg
}

// AddPost
// @Tags 岗位
// @Summary 添加岗位
// @Security ApiKeyAuth
// @Param data body system.SysPostAddReq true "添加岗位"
// @Success 200 {object} response.Response{msg=string} "{"code": 200, "msg": ""}"
// @Failure 500 {object} response.Response{msg=string} "{"code": 500, "msg": ""}"
// @Router /system/post [POST]
func (p *SysPostCtrl) AddPost(c *gin.Context) {
	postAddReq := system.SysPostAddReq{}
	if err := c.ShouldBind(&postAddReq); err != nil {
		global.MoLogger.Info("参数错误", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	postAddReq.CreatedBy = utils.GetUsernameFromGin(c)
	postAddReq.UpdatedBy = postAddReq.CreatedBy
	if ok, err := services.ServiceGroupInst.SystemGroupInst.SysPostServiceInst.AddPost(&postAddReq); err != nil {
		global.MoLogger.Info("添加岗位失败", zap.Error(err))
		response.FailMsgAsI18n(c, consts.CommCreatFailCode, consts.TKey_PostAddFailMsg)
		return
	} else if !ok {
		response.FailMsgAsI18n(c, consts.CommCreatFailCode, consts.TKey_PostAddFailMsg)
		return
	}
	response.SuccessAsI18n(c, consts.TKey_CommCreatSuccessMsg, nil)
}

//	DeletePostById
//
// @Tags 岗位
// @Summary 删除岗位
// @Security ApiKeyAuth
// @Param id path string true "岗位id"
// @Success 200 {object} response.Response{msg=string} "{"code": 200, "msg": ""}"
// @Failure 500 {object} response.Response{msg=string} "{"code": 500, "msg": ""}"
// @Router /system/post/:id [DELETE]
func (p *SysPostCtrl) DeletePostById(c *gin.Context) {
	idStrs := strings.Split(c.Param("id"), ",")
	ids := utils.StrsToUint64s(&idStrs)
	if len(*ids) == 0 {
		global.MoLogger.Info("参数错误")
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	if ok, err := services.ServiceGroupInst.SystemGroupInst.SysPostServiceInst.DeletePostByIds(*ids); err != nil {
		global.MoLogger.Info("删除岗位失败", zap.Error(err))
		response.FailMsgAsI18n(c, consts.CommDeleteFailCode, consts.TKey_PostDeleteFailMs)
		return
	} else if !ok {
		response.FailMsgAsI18n(c, consts.CommDeleteFailCode, consts.TKey_PostDeleteFailMs)
		return
	}
	response.SuccessAsI18n(c, consts.TKey_CommDeleteSuccessMsg, nil)
}

// GetPostPage
// @Tags 岗位
// @Summary 分页获取岗位列表
// @Security ApiKeyAuth
// @Param data query system.SysPostPageReq true "分页获取岗位列表"
// @Success 200 {object} response.Response{data=response.PageResult} "{"code": 200, "data": [...]}"
// @Failure 500 {object} response.Response{msg=string} "{"code": 500, "msg": ""}"
// @Router /system/post/list [GET]
func (p *SysPostCtrl) GetPostPage(c *gin.Context) {
	postPageReq := system.SysPostPageReq{}
	if err := c.ShouldBind(&postPageReq); err != nil {
		global.MoLogger.Info("参数错误", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	postPageReq.ShouldBindParams(c)
	if pageResult, err := services.ServiceGroupInst.SystemGroupInst.SysPostServiceInst.GetPostPage(&postPageReq); err != nil {
		global.MoLogger.Info("获取岗位失败", zap.Error(err))
		response.FailMsgAsI18n(c, consts.CommSelectFailCode, consts.TKey_PostGetFailMsg)
		return
	} else {
		response.SuccessAsI18n(c, consts.TKey_CommGetSuccessMsg, pageResult)
		return
	}
}

// UpdatePostById
// @Tags 岗位
// @Summary 更新岗位
// @Security ApiKeyAuth
// @Param data body system.SysPostUpdateReq true "更新岗位"
// @Success 200 {object} response.Response{msg=string} "{"code": 200, "msg": ""}"
// @Failure 500 {object} response.Response{msg=string} "{"code": 500, "msg": ""}"
// @Router /system/post [PUT]
func (p *SysPostCtrl) UpdatePostById(c *gin.Context) {
	postUpdateReq := system.SysPostUpdateReq{}
	if err := c.ShouldBind(&postUpdateReq); err != nil {
		global.MoLogger.Info("参数错误", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	postUpdateReq.UpdatedBy = utils.GetUsernameFromGin(c)
	if ok, err := services.ServiceGroupInst.SystemGroupInst.SysPostServiceInst.UpdatePostById(&postUpdateReq); err != nil {
		global.MoLogger.Info("更新岗位失败", zap.Error(err))
		response.FailMsgAsI18n(c, consts.CommUpdateFailCode, consts.TKey_PostUpdateFailMsg)
		return
	} else if !ok {
		response.FailMsgAsI18n(c, consts.CommUpdateFailCode, consts.TKey_PostUpdateFailMsg)
		return
	}
	response.SuccessAsI18n(c, consts.TKey_CommUpdateSuccessMsg, nil)
}

// GetPostById
// @Tags 岗位
// @Summary 根据id获取岗位信息
// @Security ApiKeyAuth
// @Param id path string true "岗位id"
// @Success 200 {object} response.Response{data=system.SysPostItemResp} "{"code": 200, "data": [...]}"
// @Failure 500 {object} response.Response{msg=string} "{"code": 500, "msg": ""}"
// @Router /system/post/:id [GET]
func (p *SysPostCtrl) GetPostById(c *gin.Context) {
	idStr := c.Param("id")
	id, err := strconv.ParseUint(idStr, 10, 64)
	if err != nil {
		global.MoLogger.Info("参数错误", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	if postItemResp, err := services.ServiceGroupInst.SystemGroupInst.SysPostServiceInst.GetPostById(id); err != nil {
		global.MoLogger.Info("获取岗位失败", zap.Error(err))
		response.FailMsgAsI18n(c, consts.CommSelectFailCode, consts.TKey_PostGetFailMsg)
		return
	} else {
		response.SuccessAsI18n(c, consts.TKey_CommGetSuccessMsg, postItemResp)
		return
	}
}

// ExportPost
// @Tags 岗位
// @Summary 导出岗位
// @Security ApiKeyAuth
// @Param data query system.SysPostPageReq true "导出岗位"
// @Success 200 {object} response.Response{msg=string} "{"code": 200, "msg": ""}"
// @Failure 500 {object} response.Response{msg=string} "{"code": 500, "msg": ""}"
// @Router /system/post/export [POST]
func (p *SysPostCtrl) ExportPost(c *gin.Context) {
	p.IgnoreLogResponseData(c)
	postPageReq := system.SysPostPageReq{}
	if err := c.ShouldBind(&postPageReq); err != nil {
		global.MoLogger.Info("参数错误", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	if buf, err := services.ServiceGroupInst.SystemGroupInst.SysPostServiceInst.ExportPost(&postPageReq); err != nil {
		global.MoLogger.Info("导出岗位失败", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ServerOccurredErrorCode, consts.TKey_PostExportFailMsg)
		return
	} else {
		response.SuccessWithFileData(c, fmt.Sprintf(consts2.Excel_Export_Post, time.Now().Unix()), buf)
	}
}
